<?php


namespace App\Modules\Health\Http\Controllers\Api;


use App\Http\Controllers\Api\Controller;
use App\Modules\Health\Models\Disease;
use App\Modules\Health\Models\HealthPlan;
use Illuminate\Http\Request;

class PlanController extends Controller
{
    public function list(Request $request)
    {
        $memberId = auth('api')->user()->id ?? 0;

        if (!$memberId) {
            return $this->error('未登录', [], 40001, 401);
        }

        $list = HealthPlan::where([
            'member_id' => $memberId,
            'mini_type' => $this->getMiniType(),
        ])->orderByDesc('id')->paginate();

        return $this->_success([
            'list' => $list->items()
        ]);
    }

    public function add(Request $request)
    {
        $id = $request->input('id');

        $memberId = auth('api')->user()->id ?? 0;

        if (!$id) {
            return $this->error('id不能为空');
        }

        if (!$memberId) {
            return $this->error('未登录', [], 40001, 401);
        }

        $info = Disease::with('scheme')->find($id);

        if (!$info) {
            return $this->error('信息不存在');
        }

        if (!$acuNum = count($info->scheme)) {
            return $this->error('当前暂无调理方案');
        }

        $exists = HealthPlan::where([
            'member_id'  => $memberId,
            'mini_type'  => $this->getMiniType(),
            'disease_id' => $id,
        ])->whereDate('created_at', '=', date('Y-m-d'))->exists();

        if ($exists) {
            return $this->error('您今天已经添加该健康计划');
        }

        $day = $info->scheme->max('day');

        $ret = HealthPlan::create([
            'member_id'  => $memberId,
            'disease_id' => $id,
            'acu_name'   => $info->name,
            'acu_num'    => $acuNum,
            'acu_img'    => $info->img,
            'finish_day' => 0,
            'day'        => $day > 1 ? $day : 7,
            'status'     => false,
            'mini_type'  => $this->getMiniType(),
        ]);

        return $ret ? $this->success('计划添加成功') : $this->error('计划添加失败');
    }

    public function delete(int $id)
    {
        $memberId = auth('api')->user()->id ?? 0;

        if (!$memberId) {
            return $this->error('未登录', [], 40001, 401);
        }

        $plan = HealthPlan::where([
            'id'        => $id,
            'member_id' => $memberId,
            'mini_type' => $this->getMiniType(),
        ])->first();

        if (!$plan) {
            return $this->error('信息不存在');
        }

        return $plan->delete() ? $this->success('删除成功') : $this->error('删除失败');
    }

    public function finish(Request $request)
    {
        $planId = $request->input('plan_id');
        $diseaseId = $request->input('disease_id');

        $memberId = auth('api')->user()->id ?? 0;

        if (!$memberId) {
            return $this->error('未登录', [], 40001, 401);
        }

        if (!$planId || !$diseaseId) {
            return $this->error('参数错误');
        }

        $plan = HealthPlan::where([
            'id'         => $planId,
            'disease_id' => $diseaseId,
            'mini_type'  => $this->getMiniType(),
        ])->first();

        if (!$plan) {
            return $this->error('信息不存在');
        }

        if ($plan->finish_day == $plan->day) {
            return $this->error('当前计划已完成');
        }

        if ($plan->last_finish_time) {
            $lastFinishTime = strtotime(date('Y-m-d', $plan->last_finish_time));
            if (strtotime(date('Y-m-d')) == $lastFinishTime) {
                return $this->error('当前计划今日已完成');
            }
        }

        $log = $plan->log;

        $day = $plan->finish_day + 1;

        $log[] = [
            'day'  => $day,
            'date' => date('Y-m-d H:i:s'),
        ];

        $ret = $plan->update([
            'finish_day'       => $day,
            'status'           => $day == $plan->day,
            'last_finish_time' => time(),
            'log'              => json_encode($log)
        ]);

        return $ret ? $this->success('当日计划已完成') : $this->error('当日计划完成失败');
    }
}
